#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: largest_number.py 
@version:
@time: 2024/01/13 
"""


def largest_number(nums):
    nums = [str(i) for i in nums]
    for i in range(len(nums) - 1):
        for j in range(i + 1, len(nums)):
            if int(nums[i] + nums[j]) < int(nums[j] + nums[i]):
                nums[i], nums[j] = nums[j], nums[i]

    if int("".join(nums)) == 0:
        return str(0)
    else:
        return "".join(nums)


res = largest_number([12, 121])
print(res)

# 代码优化
from functools import cmp_to_key


def largest_number2(nums):
    nums = [str(i) for i in nums]
    nums.sort(key=cmp_to_key(lambda x, y: int(y + x) - int(x + y)))
    if int("".join(nums)) == 0:
        return '0'
    else:
        return "".join(nums)


res = largest_number2([12, 121])
print(res)
